<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <el-form-item label="合同编号" prop="contract_number">
                    <el-input v-model.trim="queryParams.contract_number" placeholder="请输入合同编号" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <upload-button :action="action" @complete="getList"></upload-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" :loading="download" :disabled="download" @click="handleExport">下载模板</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" icon="el-icon-download" @click="downloadExe">打印插件下载</el-button>
                </el-col>
                <el-col :span="1.5">
                    <!-- <el-button type="primary" plain icon="el-icon-plus" @click="showDetailDialog({})">添加</el-button> -->
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table v-loading="loading" :data="tableData" border script>
                <el-table-column label="ID" prop="id" min-width="40"></el-table-column>
                <!-- <el-table-column label="合同编号" prop="contract_number" min-width="100" show-overflow-tooltip></el-table-column> -->
                <el-table-column label="合同编号" prop="contract_number" min-width="140" sortable>
                    <template slot-scope="scope">
                        <el-link type="primary" @click="showDetailDialog(scope.row)">{{ scope.row.contract_number }}</el-link>
                    </template>
                </el-table-column>
                <el-table-column label="工厂款号" prop="spu" min-width="80" show-overflow-tooltip></el-table-column>
                <el-table-column label="商品名称" prop="product_name" min-width="80" show-overflow-tooltip></el-table-column>
                <el-table-column label="发起人" prop="admin_name" min-width="80" show-overflow-tooltip></el-table-column>
                <el-table-column label="合作厂家" prop="supply_name" min-width="80" show-overflow-tooltip></el-table-column>
                <el-table-column label="合计数量" prop="purchase_nums" min-width="80" show-overflow-tooltip></el-table-column>
                <el-table-column label="交货日期" prop="give_time" min-width="80" show-overflow-tooltip></el-table-column>
                <el-table-column label="备注" prop="remark" min-width="100" show-overflow-tooltip></el-table-column>
                <el-table-column v-if="contact_url !== ''" label="合同" prop="contact_url" min-width="80" sortable>
                    <template slot-scope="scope">
                        <el-button v-if="scope.row.contact_url !== ''" type="text" icon="el-icon-download" @click="handleExportinfo(scope.row)">下载</el-button>
                    </template>
                </el-table-column>
                
                <el-table-column label="创建时间" prop="create_time" min-width="100" show-overflow-tooltip></el-table-column>
                <el-table-column label="操作" min-width="100">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="addRemark(scope.row)" >添加备注</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
        </div>
        <DetailDialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" @success="handleQuery2"></DetailDialog>

        <DetailDialogspuinfo v-model="detailDialogspuinfo.visible" v-if="detailDialogspuinfo.visible" :params="detailDialogspuinfo.data" @success="handleQueryspuinfo"></DetailDialogspuinfo>

        <shopRemarkDialog v-model="remarkDialog.visible" :params="remarkDialog.data" @success="handleQuery"></shopRemarkDialog>
    </div>
</template>

<script>
import { exportFile } from '@/utils/index'
import { querySupplierProcurementList,exportquerySupplierProcurement } from '@/api/supplierProcurement'
import { STATUS_LIST, IS_HEAD_LIST } from './constant'
import DetailDialog from './components/DetailDialog.vue'
import DetailDialogspuinfo from './components/DetailDialogspuinfo.vue'
import shopRemarkDialog from './components/shopRemarkDialog.vue'
export default {
    name: 'SupplyContact',
    components: { DetailDialog,DetailDialogspuinfo ,shopRemarkDialog},
    data() {
        return {
            supplyList: [],
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            STATUS_LIST,
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                supply_id: undefined,
                supply_name: undefined,
                spu: undefined
            },
            remarkDialog: {
                visible: false,
                data: {},
            },
            detailDialog: {
                visible: false,
                data: {},
            },
            detailDialogspuinfo: {
                visible: false,
                data: {},
            },
            dict: {
                STATUS_LIST,
                IS_HEAD_LIST,
                spuList: []
            },
            action: process.env.VUE_APP_BASE_API + '/supply/SupplyContact/importInfo',
            download: false
        }
    },
    async created() {
        this.loading = true
        try {
            // const spuData = await querySupplySpuList()
            // this.dict.spuList = spuData?.data?.list ?? []

            // const SupplyListData = await querySupplyListdown()
            // this.supplyList = SupplyListData?.data ?? []

        } catch (error) {
            this.loading = false
            console.log(error)
        }
        this.getList()
    },
    methods: {
        addRemark(row) {
            this.remarkDialog.visible = true
            this.remarkDialog.data = row
        },
        handleExportinfo(row) {
            let contractUrl = row.contact_url;
            if (contractUrl) {
                const downloadUrl = `${process.env.VUE_APP_BASE_API}${contractUrl}`;
                window.open(downloadUrl, '_blank');
            }else{
                this.$message({
                    type: 'warning',
                    message: '合同未上传'
                })
            }
        },
        handleExport() {
            this.download = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            console.log('test',qyparams);
            exportquerySupplierProcurement(qyparams)
                .then(res => {
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    this.download = false
                })
        },
        showDetailDialog(row) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
        },
        showDetailDialogspuinfo(row) {
            this.detailDialogspuinfo.visible = true
            this.detailDialogspuinfo.data = row
        },
        downloadExe() {
            exportFile({
                downloadUrl: process.env.VUE_APP_PRINT_URL,
            })
        },
        // deleteItem(row){
        //     this.$confirm('确认要删除该供应商吗?', '警告', {
        //         confirmButtonText: '确定',
        //         cancelButtonText: '取消',
        //         type: 'warning',
        //     }).then(()=>{
        //         deleteSupplyList({id: row.id}).then((res)=>{
        //             this.$message.success(res.msg)
        //             this.getList()
        //         })
        //     })
        // },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            console.log('test',qyparams);
            
            querySupplierProcurementList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 搜索按钮操作
        handleQuery2() {
            this.getList()
        },
        handleQueryspuinfo() {
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>
